<?php
/**
 * EntryPoints.php
 * 
 * Copyright (c) 2007 Maurice Fonk
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 * 
 * @category   Naneau
 * @package    Naneau_Plugin
 * @copyright  Copyright (c) 2007 Maurice Fonk - http://naneau.nl
 * @version    0.1.1
 */

/** base class */
require_once 'Zend/Controller/Plugin/Abstract.php';

/**
 * Naneau_Controller_Plugin_EntryPoints
 * 
 * set up a set of entry points
 *
 * @category   Naneau
 * @package    Naneau_Plugin
 * @copyright  Copyright (c) 2007 Maurice Fonk - http://naneau.nl
 */
class Naneau_Controller_Plugin_EntryPoints extends Zend_Controller_Plugin_Abstract {

    /**
     * array of entry points,
     * [paramName] => array('default' => 'defaultValue', 'values' => array())
     *
     * @var unknown_type
     */
    protected $map = array();

    /**
     * add an entry point
     *
     * @param string $name
     * @param array $values
     * @param string $default
     */
    public function addEntryPoint($name, $values, $default) {
        $this->map[$name] = array(
        'default' => $default,
        'values' => $values
        );
    }

    /**
     * route startup hook
     */
    public function routeStartup(Zend_Controller_Request_Abstract $request) {
        $frontController = Zend_Controller_Front::getInstance();
        //the front controller
        $baseUrl = $frontController->getBaseUrl();
        //the base url (minus starting slash)
        $request = $frontController->getRequest();
        //the original request

        $uri = $request->getRequestUri();
        //complete uri
        $uri = substr($uri, strlen($baseUrl));
        //strip base url and starting slash

        foreach ($this->map as $name => $entrypoint) {
            //per entry point set
            $value = $entrypoint['default'];
            //start with default value
            foreach ($entrypoint['values'] as $search) {
                //try each entry point value
                if (strpos($uri, '/' . $search . '/') === 0) {
                    //we found one!
                    $value = $search;
                    //update the value
                    $uri = substr($uri, strlen($search) + 1);
                    //strip the result from the uri
                }
            }
            $this->getRequest()->setParam($name, $value);
            //set the entry point as a request param
        }
        //find entry points and strip them from the url, set the FC params

        $uri = $baseUrl . $uri;
        //rebuild uri
        $request->setRequestUri($uri);
        //reset the request uri
    }

    /**
     * Adds entrypoints from a Zend_Config object
     *
     * @param Zend_Config $config
     * @param string $section The section to load the config from
     */
    public function addConfig(Zend_Config $config, $section) {
        if ($config->{$section} === null) {
            throw new Zend_Exception("No entrypoint configuration in section '{$section}'");
        }
        foreach($config->{$section} as $name => $info) {
            $this->addEntryPoint($name, $info->values->toArray(), $info->default);
        }
    }

}